
import { apps } from './app.js'; //这个是全局变量 可以自由导入
// app status
export const NOT_LOADED = 'NOT_LOADED'; // 应用未加载
export const LOADING_SOURCE_CODE = 'LOADING_SOURCE_CODE'; // 应用正在加载
export const LOAD_ERROR = 'LOAD_ERROR'; // 应用加载错误

// 启动的过程
export const NOT_BOOTSTRAPPED = 'NOT_BOOTSTRAPPED'; // 资源加载完毕 需要启动 此时还没启动
export const BOOTSTRAPPING = 'BOOTSTRAPPING'; // 启动中
export const NOT_MOUNTED = 'NOT_MOUNTED'; // 启动完毕 还没挂载

// 挂载流程
export const MOUNTING = 'MOUNTING'; // 挂载中
export const MOUNTED = 'MOUNTED'; // 挂载完毕

// 卸载流程
export const UNLOADING = 'UNLOADING'; // 卸载中


// 应用是否被激活--激活就是挂载
export function isActive(app) {
    return app.status === MOUNTED;
}

// 判断应用要不要激活
export function shouldBeActive(app) {
    return app.activeWhen(window.location);
}

// 获取应用的变化
export function getAppChanges() {
    const appsToLoad = [];
    const appsToMount = [];
    const appsToUnmount = [];

    apps.forEach(app => {
     let appShouldBeActive = shouldBeActive(app);

     switch (app.status) {
         case NOT_LOADED:
         case LOADING_SOURCE_CODE:
             if (appShouldBeActive) {
                 // 需要启动的应用
                 appsToLoad.push(app);
             }
             break;
         case NOT_BOOTSTRAPPED:
         case BOOTSTRAPPING:
         case NOT_MOUNTED:
             if (appShouldBeActive) {
                 // 需要挂载的应用
                 appsToMount.push(app);
             }
             break;
         case MOUNTED:
            // 路径没匹配上
             if (!appShouldBeActive) {
                 // 需要卸载的应用
                 appsToUnmount.push(app);
             }
             break;
          default:
             // 其他状态不处理
             break;
     }

    })

    return {
        appsToLoad,
        appsToMount,
        appsToUnmount
    };
}